using Furion.RemoteRequest.Extensions;
using Org.BouncyCastle.Utilities;
using SkiaSharp;
using SQLitePCL;
using System.Drawing;
using System.Text.RegularExpressions;
using ViewFaceCore.Model;

namespace Admin.NET.Plugin.ViewFace;

/// <summary>
/// 工具类
/// </summary>
public static class FaceUtil
{
    /// <summary>
    /// 图片base64转byte[]
    /// </summary>
    /// <param name="base64"></param>
    /// <returns></returns>
    public async static Task<byte[]> GetFaceData(string base64)
    {
        //如果是url地址自动下载
        return !IsUrl(base64) ? Convert.FromBase64String(base64) : await base64.GetAsByteArrayAsync();
    }
    // <summary>
    /// 识别urlStr是否是网络路径
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public static bool IsUrl(string url)
    {
        if (Regex.IsMatch(url, @"((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    /// <summary>
    /// 生成图片小图
    /// </summary>
    /// <param name="bitmap"></param>
    /// <param name="faceInfo"></param>
    /// <returns></returns>
    public static string GetCorpImage(SKBitmap bitmap, FaceInfo faceInfo)
    {
        var corp_image_base64 = "";
        // 裁剪人脸小图
        int cropOffset = 10;
        int x = faceInfo.Location.X - cropOffset;
        int y = faceInfo.Location.Y - cropOffset;
        int w = faceInfo.Location.Width + cropOffset * 2;
        int h = faceInfo.Location.Height + cropOffset * 2;

        // 创建一个新的Bitmap来保存裁剪后的图片
        using (var croppedBitmap = new SKBitmap(w, h))
        {
            // 创建Canvas并将裁剪区域绘制到新Bitmap上
            using (var newcanvas = new SKCanvas(croppedBitmap))
            {
                newcanvas.DrawBitmap(bitmap, new SKRect(x, y, x + w, y + h), new SKRect(0, 0, w, h));
            }
            using (var data = SKImage.FromBitmap(croppedBitmap).Encode(SKEncodedImageFormat.Jpeg, 100))
            {
                corp_image_base64 = Convert.ToBase64String(data.ToArray());
            }
        }
        //
        return corp_image_base64;
    }
    /// <summary>
    /// 图片转base64
    /// </summary>
    /// <param name="bitmap"></param>
    /// <returns></returns>
    public static string BitmapToBase64(SKBitmap bitmap)
    {
        using (var image = SKImage.FromBitmap(bitmap))
        using (var data = image.Encode(SKEncodedImageFormat.Png, 100))
        {
            return Convert.ToBase64String(data.ToArray());
        }
    }
    /// <summary>
    /// base64字符串转图片
    /// </summary>
    /// <param name="base64"></param>
    /// <returns></returns>
    public static SKBitmap Base64ToBitmap(string base64)
    {
        byte[] imageBytes = Convert.FromBase64String(base64);
        SKBitmap bitmap;
        using (SKImage image = SKImage.FromEncodedData(imageBytes))
        {
            bitmap = SKBitmap.FromImage(image);
        }
        return bitmap;
    }

    #region "Base64、byte数组、float数组之间相互转换"
    /// <summary>
    /// 将byte数组转换为Base64字符串
    /// </summary>
    /// <param name="byteArray"></param>
    /// <returns></returns>
    public static string ConvertToString(byte[] byteArray)
    {
        return Convert.ToBase64String(byteArray);
    }
    /// <summary>
    /// 将float数组转换为Base64字符串
    /// </summary>
    /// <param name="floatArray"></param>
    /// <returns></returns>
    public static string ConvertToString(float[] floatArray)
    {
        var byteArray = ConvertToByteArray(floatArray);
        return Convert.ToBase64String(byteArray);
    }
    /// <summary>
    /// 将byte数组转换为float数组
    /// </summary>
    /// <param name="byteArray"></param>
    /// <returns></returns>
    public static float[] ConvertToFloatArray(byte[] byteArray)
    {
        // 假设byteArray的长度能够被4整除
        float[] floatArray = new float[byteArray.Length / 4];
        for (int i = 0; i < floatArray.Length; i++)
        {
            floatArray[i] = BitConverter.ToSingle(byteArray, i * 4);
        }
        return floatArray;
    }

    /// <summary>
    /// 将Base64字符串解码为byte数组
    /// </summary>
    /// <param name="base64String"></param>
    /// <returns></returns>
    public static byte[] ConvertToByteArray(string base64String)
    {
        return Convert.FromBase64String(base64String);
    }
    /// <summary>
    /// 将Base64字符串解码为float数组
    /// </summary>
    /// <param name="base64String"></param>
    /// <returns></returns>
    public static float[] ConvertToFloatArray(string base64String)
    {
        var byteArray=ConvertToByteArray(base64String);
        return ConvertToFloatArray(byteArray);
    }
    /// <summary>
    /// 将float数组转换回byte数组
    /// </summary>
    /// <param name="floatArray"></param>
    /// <returns></returns>
    public static byte[] ConvertToByteArray(float[] floatArray)
    {
        byte[] byteArray = new byte[floatArray.Length * 4];
        for (int i = 0; i < floatArray.Length; i++)
        {
            byte[] bytes = BitConverter.GetBytes(floatArray[i]);
            Array.Copy(bytes, 0, byteArray, i * 4, 4);
        }
        return byteArray;
    }
    #endregion
}